﻿using System;
using FunctionalCLR.Core;
using FunctionalCLR.Shell;
using NUnit.Framework;

namespace Zove.Test
{
    public abstract class AbstractTests
    {
        public IContext Execute(string source)
        {
            Interpreter intp = new Interpreter(new ShellContext());
            intp.Execute(source);

            return intp.Context;
        }

        protected static Parser GetParser(string expr)
        {
            Parser p = new Parser(expr);
            p.ConstantManager
                = DefaultDependencyManager.Instance.ConstantManager;
            p.FunctionManager
                = DefaultDependencyManager.Instance.FunctionManager;
            return p;
        }

        public void TestExpression(string expr, double expected)
        {
            Parser p = GetParser(expr);
            Node root = p.Parse();

            double result = Convert.ToDouble(root.Evaluate());
            Assert.AreEqual(
                Math.Round(expected, 5), Math.Round(result, 5));
        }

        public void TestExpression(string expr, string expected)
        {
            Parser p = GetParser(expr);
            Node root = p.Parse();

            string result = Convert.ToString(root.Evaluate());
            Assert.AreEqual(result, expected);
        }

        public void TestExpression(string expr, bool expected)
        {
            Parser p = GetParser(expr);
            Node root = p.Parse();

            object re = root.Evaluate();
            bool result = Convert.ToBoolean(re);
            Assert.IsTrue(result == expected);
        }

    }
}
